<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 11:43
 */

namespace admin\controllers;

use admin\models\Goods;
use admin\models\GoodsCate;
use admin\models\SellGoods;
use common\helpers\FileHelper;
use common\widgets\UploadedFile;

class GoodsController extends \admin\components\Controller
{
    public function actionList()
    {
        $query = (new Goods())->search()->joinWith(['cate'])->orderBy('sort ASC');
        $html = $query->getTable([
            'goods_id',
            'goods_name' => ['type' => 'text'],
            'cate.name' => ['search' => true, 'header' => '商品分类'],
            'amount' => ['type' => 'text'],
            'score' => ['type' => 'text'],
            'sort' => ['type' => 'text'],
            'state' => ['search' => 'select', 'type' => 'select'],
            'created_at',
            ['type' => ['edit' => 'saveGoods', 'delete' => "ajaxDeleteGoods"]]
        ], [
            'searchColumns' => [
                'goods_id',
                'goods_name',
            ],
            'paging' => 20,
            'addBtn' => ['createGoods' => '添加商品'],
        ]);

        return $this->render('list', compact('html'));
    }

    public function actionCreateGoods($id = null)
    {
        $goods = Goods::findModel($id);
        if($goods->load()){
            if (isset($_FILES['Goods']['name']['images'])) {
                $goods->images = UploadedFile::getInstance($goods, 'images');
                if ($goods->images) {
                    $savePath = \Yii::getAlias('@webroot' . config('uploadPath') .  '/image/' . date('Ymd') . '/');
                    FileHelper::mkdir($savePath);
                    do {
                        $randName = date('His') . rand(100000, 999999) . '.' . $goods->images->extension;
                    } while (file_exists($savePath . $randName));
                    $saveRes = $goods->images->saveAs($savePath . $randName);
                    if($saveRes) $goods->img = config('uploadPath') .  '/image/' . date('Ymd') . '/'  . $randName;
                }
            }
            if($goods->validate()){
                $goods->insert();
                return success();
            }else{
                return error($goods);
            }
        }
        return $this->render('createGoods', compact('goods'));
    }

    public function actionSaveGoods($id = null)
    {
        $goods = Goods::findModel($id);
        if($goods->load()){
            if (isset($_FILES['Goods']['name']['images'])) {
                $goods->images = UploadedFile::getInstance($goods, 'images');
                if ($goods->images) {
                    $savePath = \Yii::getAlias('@webroot' . config('uploadPath') .  '/image/' . date('Ymd') . '/');
                    FileHelper::mkdir($savePath);
                    do {
                        $randName = date('His') . rand(100000, 999999) . '.' . $goods->images->extension;
                    } while (file_exists($savePath . $randName));
                    $saveRes = $goods->images->saveAs($savePath . $randName);
                    if($saveRes) $goods->img = config('uploadPath') .  '/image/' . date('Ymd') . '/' . $randName;
                }
            }
            if($goods->validate()){
                $goods->update();
                return success();
            }else{
                return error($goods);
            }
        }
        return $this->render('saveGoods', compact('goods'));
    }

    public function actionAjaxDeleteGoods()
    {
        $id = post("id");
        $goods = Goods::findModel($id);
        if($goods && $goods->delete()){
            return success();
        }else{
            return self::error($goods);
        }
    }

    public function actionCate()
    {
        $query = (new GoodsCate())->listQuery()->orderBy('hot ASC');
        $html = $query->getTable([
            'id',
            'name' => ['type' => 'text'],
            'state' => ['type' => 'select'],
            'hot' => ['type' => 'text'],
        ], [
            'paging' => 20,
            'addBtn' => ['createGoodsCate' => '添加商品分类'],
        ]);

        return $this->render('cate', compact('html'));
    }

    public function actionCreateGoodsCate($id = null)
    {
        $cate = GoodsCate::findModel($id);
        if($cate->load()){
            if($cate->validate()){
                $cate->hot = is_numeric($cate->hot) ? $cate->hot : 50;
                $cate->insert();
                return success();
            }else{
                return error($cate);
            }
        }
        return $this->render('createCate', compact('cate'));
    }

    public function actionSellList()
    {
        $query = (new SellGoods())->search()->orderBy('sort ASC');
        $html = $query->getTable([
            'goods_id',
            'goods_name' => ['type' => 'text'],
            'amount' => ['type' => 'text'],
            'score' => ['type' => 'text'],
            'sort' => ['type' => 'text'],
            'state' => ['search' => 'select', 'type' => 'select'],
            'created_at',
            ['type' => ['edit' => 'saveSellGoods', 'delete' => "ajaxDeleteSellGoods"]]
        ], [
            'searchColumns' => [
                'goods_id',
                'goods_name',
            ],
            'paging' => 20,
            'addBtn' => ['createSellGoods' => '添加代销商品'],
        ]);

        return $this->render('sellList', compact('html'));
    }

    public function actionCreateSellGoods($id = null)
    {
        $goods = SellGoods::findModel($id);
        if($goods->load()){
            if (isset($_FILES['SellGoods']['name']['images'])) {
                $goods->images = UploadedFile::getInstance($goods, 'images');
                if ($goods->images) {
                    $savePath = \Yii::getAlias('@webroot' . config('uploadPath') .  '/image/' . date('Ymd') . '/');
                    FileHelper::mkdir($savePath);
                    do {
                        $randName = date('His') . rand(100000, 999999) . '.' . $goods->images->extension;
                    } while (file_exists($savePath . $randName));
                    $saveRes = $goods->images->saveAs($savePath . $randName);
                    if($saveRes) $goods->img = config('uploadPath') .  '/image/' . date('Ymd') . '/' .  $randName;
                }
            }
            if($goods->validate()){
                $goods->insert();
                return success();
            }else{
                return error($goods);
            }
        }
        return $this->render('createSellGoods', compact('goods'));
    }

    public function actionSaveSellGoods($id = null)
    {
        $goods = SellGoods::findModel($id);
        if($goods->load()){
            if (isset($_FILES['SellGoods']['name']['images'])) {
                $goods->images = UploadedFile::getInstance($goods, 'images');
                if ($goods->images) {
                    $savePath = \Yii::getAlias('@webroot' . config('uploadPath') .  '/image/' . date('Ymd') . '/');
                    FileHelper::mkdir($savePath);
                    do {
                        $randName = date('His') . rand(100000, 999999) . '.' . $goods->images->extension;
                    } while (file_exists($savePath . $randName));
                    $saveRes = $goods->images->saveAs($savePath . $randName);
                    if($saveRes) $goods->img = config('uploadPath') .  '/image/' . date('Ymd') . '/' . $randName;
                }
            }
            if($goods->validate()){
                $goods->update();
                return success();
            }else{
                return error($goods);
            }
        }
        return $this->render('saveSellGoods', compact('goods'));
    }

    public function actionAjaxDeleteSellGoods()
    {
        $id = post("id");
        $goods = SellGoods::findModel($id);
        if($goods && $goods->delete()){
            return success();
        }else{
            return self::error($goods);
        }
    }
}